package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.exception.PasswordErrorException;
import com.sky.properties.JwtProperties;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.BcriptUtils;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 员工分页查询
     */
    @GetMapping("/page")
    public Result pageEmp(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("员工登录" + employeePageQueryDTO.getName());

        PageBean pageBean = employeeService.getEmployees(employeePageQueryDTO);
        return Result.success(pageBean);

    }

    // 添加员工
    @PostMapping
    public Result addEmp(@RequestBody Employee emp) {
        employeeService.addEmp(emp);
        return Result.success();
    }

    //根据ID查询员工信息
    @GetMapping("/{id}")
    public Result getEmpById(@PathVariable String id) {
        Employee emp = employeeService.getEmp(id);
        return Result.success(emp);
    }

    // 修改员工信息
    @PutMapping
    public Result updateEmp(@RequestBody Employee emp) {
        employeeService.updateEmp(emp);
        return Result.success();
    }

    // 启用、禁用员工账号
    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable String status, String id) {
        employeeService.changeStatus(status, id);
        return Result.success();
    }

    // 修改员工密码
    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody PasswordEditDTO password) {
        if (employeeService.editPassword(password)) {
            // 颁发新令牌
            Long currentId = BaseContext.getCurrentId();
            password.setEmpId(currentId);
            Employee emp = employeeService.getEmp(String.valueOf(password.getEmpId()));
            Map<String, Object> map = new HashMap<>();
            map.put("empId", emp.getId());
//            map.put("username",emp.getUsername());
//            map.put("name",emp.getName());
            String jwt = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(),
                    jwtProperties.getAdminTtl(), map);// 颁发令牌

            return Result.success(jwt);
        } else {

           throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

    }
}
